package com.rmxx.fruit.fragment;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.company.android.fruit.R;
import com.kwan.xframe.common.widget.observablescrollview.ObservableRecyclerView;
import com.kwan.xframe.common.widget.observablescrollview.ObservableScrollViewCallbacks;
import com.kwan.xframe.mvp.view.fragment.BasePageItemFragment;
import com.rmxx.fruit.activity.MainActivity;
import com.rmxx.fruit.activity.ShopActivity;
import com.rmxx.fruit.bean.Product;
import com.rmxx.fruit.bean.Shop;

import java.util.ArrayList;
import java.util.List;

public class ShopFragment extends BasePageItemFragment {

	protected ObservableRecyclerView mRecyclerView;
	protected ObservableScrollViewCallbacks observableScrollViewCallbacks;
	protected BaseQuickAdapter<Shop, BaseViewHolder> mBaseAdapter;
	protected int pageIndex = 0;
	protected int pageSize = 20;
	protected boolean isLoadMore = false;

	private List<Shop> mShops = new ArrayList<>();

	public ShopFragment() {
		// Required empty public constructor
	}

	public static ShopFragment newInstance() {
		ShopFragment fragment = new ShopFragment();
		Bundle args = new Bundle();
		fragment.setArguments(args);
		return fragment;
	}


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mRecyclerView = mContentView.findViewById(R.id.recycleView);

		mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
		mRecyclerView.setItemAnimator(new DefaultItemAnimator());
		mRecyclerView.setScrollViewCallbacks(observableScrollViewCallbacks);
		mRecyclerView.setHasFixedSize(true);
		mRecyclerView.setNestedScrollingEnabled(false);

		//int spanCount = 2;
		//int spacing = 20;
		//mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, true));
		//麻痹的 关闭硬件加速 否则 cardView 报错

		mRecyclerView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		mRecyclerView.setLayerType(View.LAYER_TYPE_HARDWARE, null);


		Shop shop = new Shop();
		shop.setRlogo(R.mipmap.shop_logo1);
		shop.setName("渔家码头生鲜馆");
		List<Product> productList = new ArrayList<>();
		Product product = new Product();
		product.setRicon(R.mipmap.product1_01);
		productList.add(product);
		product = new Product();
		product.setRicon(R.mipmap.product1_02);
		productList.add(product);
		product = new Product();
		product.setRicon(R.mipmap.product1_03);
		productList.add(product);
		shop.setProducts(productList);

		mShops.add(shop);
		shop = new Shop();
		shop.setRlogo(R.mipmap.shop_logo2);
		shop.setName("果然生鲜旗舰店");
		productList = new ArrayList<>();
		product = new Product();
		product.setRicon(R.mipmap.product1_04);
		productList.add(product);
		product = new Product();
		product.setRicon(R.mipmap.product1_05);
		productList.add(product);
		product = new Product();
		product.setRicon(R.mipmap.product1_06);
		productList.add(product);
		shop.setProducts(productList);


		mShops.add(shop);

		mBaseAdapter = new BaseQuickAdapter<Shop, BaseViewHolder>(R.layout.list_item_shop, mShops) {
			@Override
			protected void convert(BaseViewHolder helper, Shop item) {
				helper.setImageResource(R.id.iv_logo, item.getRlogo());
				helper.setText(R.id.tv_name, item.getName());
				helper.setImageResource(R.id.iv_1, item.getProducts().get(0).getRicon());
				helper.setImageResource(R.id.iv_2, item.getProducts().get(1).getRicon());
				helper.setImageResource(R.id.iv_3, item.getProducts().get(2).getRicon());
			}
		};

		mBaseAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
				go2Activity(ShopActivity.class, null, false);
			}
		});

		mRecyclerView.setAdapter(mBaseAdapter);

		mContentView.findViewById(R.id.v_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				((MainActivity)getActivity()).vp_content.setCurrentItem(0);
			}
		});
	}

	@Override
	protected int getContentViewId() {
		return R.layout.fragment_shop;
	}


	@Override
	public String getPageName() {
		return null;
	}


	@Override
	public void fetchData() {

	}
}
